package com.aeoj.clientmanagement.strategy.impl;

import com.aeoj.clientmanagement.domain.judge.JudgeResult;
import com.aeoj.clientmanagement.domain.problem.Problem;
import com.aeoj.clientmanagement.domain.problemrecord.ProblemRecord;
import com.aeoj.clientmanagement.enums.problem.ProblemRrecordStatusEnum;
import com.aeoj.clientmanagement.strategy.JudgeStrategy;
import com.aeoj.clientmanagement.strategy.JudgeStrategyContext;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;


import java.util.List;

/**
 * author: ZSM
 * time: 2025-03-01 22:53
 */
public class JavaJudgeStrategy implements JudgeStrategy {
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public JudgeResult doJudge(JudgeStrategyContext judgeStrategyContext) {
        List<String> inputsContext = judgeStrategyContext.getInputs();
        List<String> outputsContext = judgeStrategyContext.getOutputs();
        Long timeContext = judgeStrategyContext.getTime();
        Long memoryContext = judgeStrategyContext.getMemory();
        Problem problemContext = judgeStrategyContext.getProblem();
        ProblemRecord problemRecordContext = judgeStrategyContext.getProblemRecord();

        List<String> inputCases = null;
        List<String> outputCases = null;
        try {
            outputCases = objectMapper.readValue(problemContext.getOutputCases(), List.class);
            inputCases = objectMapper.readValue(problemContext.getInputCases(), List.class);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        Integer timeLimit = problemContext.getTimeLimit();
        Integer memoryLimit = problemContext.getMemoryLimit() * 1024 * 1024;

        JudgeResult judgeResult = new JudgeResult();
        judgeResult.setMemory(memoryContext);
        judgeResult.setTime(timeContext);
        judgeResult.setInput(String.valueOf(inputCases));
        judgeResult.setOutput(String.valueOf(outputsContext));

        Long JAVA_MORE_TIME = 10 * 1000L;
        // 定义一个判题结果枚举变量
        ProblemRrecordStatusEnum problemRrecordStatusEnum = ProblemRrecordStatusEnum.ACCEPTED;
        if (outputsContext.size() != inputsContext.size()) {
            problemRrecordStatusEnum = ProblemRrecordStatusEnum.WRONG_ANSWER;
        } else if (timeContext - JAVA_MORE_TIME > timeLimit) {
            problemRrecordStatusEnum = ProblemRrecordStatusEnum.TIME_LIMIT_EXCEEDED;
        } else if (memoryContext > memoryLimit) {
            problemRrecordStatusEnum = ProblemRrecordStatusEnum.MEMORY_LIMIT_EXCEEDED;
        } else for (int i = 0; i < outputsContext.size(); i++) {
            if (!outputsContext.get(i).equals(outputCases.get(i))) {
                problemRrecordStatusEnum = ProblemRrecordStatusEnum.WRONG_ANSWER;
                break;
            }
        }

        // 设置具体的判题结果信息
        judgeResult.setMessage(problemRrecordStatusEnum.getDescription());
        judgeResult.setStatus(problemRrecordStatusEnum.getCode());

        // 返回判题信息
        return judgeResult;
    }
}
